﻿using System;
using System.Collections.Generic;
using System.Text;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.FAS.Runtime.Metadata.Entity;
using T.FAS.Runtime.Metadata.Client;
using T.FAS.Runtime.Metadata.SPI;

namespace T.FAS.Runtime.DataModel.Plugin
{
    public class DataModelRTPUtil
    {
        private static IMetaDataClientService metaDataClientService = MetaDataClientService.GetInstance();

        /// <summary>
        /// 运行时获取CDO元数据
        /// </summary>
        /// <param name="cdoID"></param>
        /// <returns></returns>
        public static CompositeDataObject GetCompositeDataObject(string cdoID)
        {
            try
            {
                var cdo = metaDataClientService.GetMetaData(cdoID).Content as CompositeDataObject;
                if (cdo == null)
                    throw new FasException(DataModelErrorCode.RTP.Error, $"运行时获取CDO:{cdoID}实例为空");
                return cdo;
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.RTP.Error, $"运行时获取CDO:{cdoID}实例失败", e);
            }
        }

        public static T GetRTPMetaData<T>(string metaDataID) where T: IMetaDataContent
        {
            try
            {
                var metaData = (T)metaDataClientService.GetMetaData(metaDataID).Content;
                if (metaData == null)
                    throw new FasException(DataModelErrorCode.RTP.Error, $"运行时获取元数据:{metaDataID}实例为空");
                return metaData;
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.RTP.Error, $"运行时获取元数据:{metaDataID}实例失败", e);
            }
        }
    }
}
